package algorithm.poj.p3000;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;


/**
 * 分析：
 * 因为变量只有p, q, r, s, t五个，只要直接验证这32中情况就行 
 * 
 * 实现：
 * 对每种可能的赋值(p, q, r, s, t)，可以使用堆栈计算表达式的值。
 * 
 * 需要用两个堆栈(表达式栈和数值栈)模拟运算
 * 
 * 经验：
 * 1. 需要特别注意N具有较高优先级，比如ANpp，ANNNNpp也是合法的。
 * 2. NNNNNNNNNNNNNNNNNNNAKKKEEKEEKppppppppppp也是合法表达式
 * 
 * 教训：测试用例非常重要！！！
 * 
 * @author wong.tong@gmail.com
 *
 */
public class P3295 {

	public static void main(String[] args) throws Exception {

//		long start = System.currentTimeMillis();
		InputStream input = null;
		if (false) {
			input = System.in;
		} else {
			URL url = P3295.class.getResource("P3295.txt");
			File file = new File(URLDecoder.decode(url.getPath(), "UTF-8"));
			input = new FileInputStream(file);
		}
		
		BufferedReader stdin = new BufferedReader(new InputStreamReader(input));

		String line = stdin.readLine();
        while (line != null && !"0".equals(line.trim())) {
            System.out.println(tautology(line));
            line = stdin.readLine();
        }
        
//        System.out.println((System.currentTimeMillis()-start) + " ms");
	}

	private static String tautology(String s) {
		
		Map<String, Boolean> variables = new HashMap<String, Boolean>();
		for (int i = 0; i < 32; i ++) {
			variables.put("p", (i & 0x1) == 0x1);
			variables.put("q", (i & 0x2) == 0x2);
			variables.put("r", (i & 0x4) == 0x4);
			variables.put("s", (i & 0x8) == 0x8);
			variables.put("t", (i & 0x10) == 0x10);
			
			LinkedList<Object> exp = new LinkedList<Object>();
			LinkedList<Boolean> val = new LinkedList<Boolean>();
			char[] cs = s.toCharArray();
			for (int j = 0; j < cs.length; j ++) {
				String key = String.valueOf(cs[j]);
				if (variables.containsKey(key)) {
					exp.push(variables.get(key));
				} else {
					exp.push(key);
				}
			}
			
			//calculate current expression
			while (!exp.isEmpty()) {
				
				Object op = null;
				while (!exp.isEmpty() && (op = exp.pop()) instanceof Boolean) {
					val.push((Boolean)op);
				}
				if (op instanceof String) {
					
					if ("N".equals(op)) {
						Boolean w = val.pop();
						val.push(!w);
					} else {
						Boolean w = val.pop();
						Boolean x = val.pop();
						if ("K".equals((String)op)) {
							val.push(w && x);
						} else if ("A".equals((String)op)) {
							val.push(w || x);
						} else if ("C".equals((String)op)) {
							val.push(!w || x);
						} else if ("E".equals((String)op)) {
							val.push(w.equals(x));
						} else {
							throw new RuntimeException("Syntax Error!");
						}
					}
				}
			}
			
			if (!((Boolean)val.pop())) {
				return "not";
			}
		}
		return "tautology";
	}
}
